package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatActivity;
import com.happy.cat.project.system.service.ICatActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 活动管理Controller
 *
 * @author verne
 * @date 2020-07-18
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/activity" )
@Api("活动管理 API")
public class CatActivityController extends BaseController {

    private final ICatActivityService iCatActivityService;

    /**
     * 查询活动管理列表
     */
    @ApiOperation(value = "查询活动管理列表",notes = "查询活动管理列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatActivity catActivity)
    {
        startPage();
        LambdaQueryWrapper<CatActivity> lqw = new LambdaQueryWrapper<CatActivity>();
        if (StringUtils.isNotBlank(catActivity.getTitle())){
            lqw.eq(CatActivity::getTitle ,catActivity.getTitle());
        }
        if (catActivity.getStartData() != null){
            lqw.eq(CatActivity::getStartData ,catActivity.getStartData());
        }
        if (catActivity.getOverData() != null){
            lqw.eq(CatActivity::getOverData ,catActivity.getOverData());
        }
        if (StringUtils.isNotBlank(catActivity.getTitleImg())){
            lqw.eq(CatActivity::getTitleImg ,catActivity.getTitleImg());
        }
        if (StringUtils.isNotBlank(catActivity.getToLink())){
            lqw.eq(CatActivity::getToLink ,catActivity.getToLink());
        }
        if (StringUtils.isNotBlank(catActivity.getRemarks())){
            lqw.eq(CatActivity::getRemarks ,catActivity.getRemarks());
        }
        if (catActivity.getCreateDate() != null){
            lqw.eq(CatActivity::getCreateDate ,catActivity.getCreateDate());
        }
        if (catActivity.getStatus() != null){
            lqw.eq(CatActivity::getStatus ,catActivity.getStatus());
        }
        List<CatActivity> list = iCatActivityService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出活动管理列表
     */
    @ApiOperation(value = "导出活动管理列表",notes = "导出活动管理列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:activity:export')" )
    @Log(title = "活动管理" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatActivity catActivity) {
        LambdaQueryWrapper<CatActivity> lqw = new LambdaQueryWrapper<CatActivity>(catActivity);
        List<CatActivity> list = iCatActivityService.list(lqw);
        ExcelUtil<CatActivity> util = new ExcelUtil<CatActivity>(CatActivity. class);
        return util.exportExcel(list, "activity" );
    }

    /**
     * 获取活动管理详细信息
     */
    @ApiOperation(value = "获取活动管理详细信息",notes = "获取活动管理详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:activity:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatActivityService.getById(id));
    }

    /**
     * 新增活动管理
     */
    @ApiOperation(value = "新增活动管理",notes = "新增活动管理",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:activity:add')" )
    @Log(title = "活动管理" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatActivity catActivity) {
        catActivity.setCreateDate(new Date());
        catActivity.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatActivityService.save(catActivity) ? 1 : 0);
    }

    /**
     * 修改活动管理
     */
    @ApiOperation(value = "修改活动管理",notes = "修改活动管理",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:activity:edit')" )
    @Log(title = "活动管理" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatActivity catActivity) {
        return toAjax(iCatActivityService.updateById(catActivity) ? 1 : 0);
    }

    /**
     * 删除活动管理
     */
    @ApiOperation(value = "删除活动管理",notes = "删除活动管理",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:activity:remove')" )
    @Log(title = "活动管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatActivityService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
